package Handler;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.CategoriaDao;
import Modelo.Categoria;


@WebServlet("/CategoriaHandler")
public class CategoriaHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static String ADD = "/CategoriaAdd.jsp";
    private static String Edit = "/Categoriaedit.jsp";
    private static String CategoriaRecord="/ListCategorias.jsp";
    private CategoriaDao dao;
    
    public CategoriaHandler() {
        super();
        dao = new CategoriaDao();
    }
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";
        String catId = request.getParameter("categoriaid");        
        String action = request.getParameter("action");
        
        if(action.equalsIgnoreCase("insert"))
        {
                Categoria objcategoria = new Categoria();
                objcategoria.setNombre(request.getParameter("Nombre"));
                objcategoria.setDescripcion(request.getParameter("Descripcion"));
                dao.addCategoria(objcategoria);
                redirect = CategoriaRecord;
                request.setAttribute("Categorias", dao.getAllCategorias());    
                System.out.println("Record Added Successfully");
        }
        
        else if (action.equalsIgnoreCase("delete")){
            String cat_Id = request.getParameter("categoriaId");
            int catid = Integer.parseInt(cat_Id);
            dao.removeCategoria(catid);
            redirect = CategoriaRecord;
            request.setAttribute("categorias", dao.getAllCategorias());
            System.out.println("Record Deleted Successfully");
        
        }else if (action.equalsIgnoreCase("editform")){         
                redirect = Edit;            
        }
		else if (action.equalsIgnoreCase("addnew")){         
		        redirect = ADD;            
		}
        else if (action.equalsIgnoreCase("edit")){
                String cat_Id = request.getParameter("CategoriaId");
            int catid = Integer.parseInt(cat_Id);            
            Categoria objcategoria = new Categoria();
            objcategoria.setCategoriaId(catid);
            objcategoria.setNombre(request.getParameter("Nombre"));
            objcategoria.setDescripcion(request.getParameter("Descripcion"));
            dao.editCategoria(objcategoria);
            request.setAttribute("categoria", objcategoria);
            redirect = CategoriaRecord;
            System.out.println("Record updated Successfully");
            
         } else if (action.equalsIgnoreCase("listCategorias")){
            redirect = CategoriaRecord;
            request.setAttribute("categorias", dao.getAllCategorias());
         } else {
            redirect = ADD;
        }

        RequestDispatcher rd = request.getRequestDispatcher(redirect);
        rd.forward(request, response);
	
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
			
	}

}
